package nth.bananas.solving;

import java.util.HashSet;
import java.util.Set;


public class PuzzleUtils {

	public static Set<String> findWords(Puzzle p) {
		Set<String> result = new HashSet<String>();
		
		for (GridSpot<Letter> gs : p.getSpots()) {
			if (! p.spotExists(gs.getX() - 1, gs.getY()) && p.spotExists(gs.getX() + 1, gs.getY())) {
				result.add(readWord(gs, Direction.ACROSS, p));
			}
			if (! p.spotExists(gs.getX(), gs.getY() + 1) && p.spotExists(gs.getX(), gs.getY() - 1)) {
				result.add(readWord(gs, Direction.DOWN, p));
			}
		}
		
		return result;
	}

	private static String readWord(final GridSpot<Letter> gs, final Direction whichWay, Puzzle p) {
		String result = "";
		
		GridSpot<Letter> tile = gs;
		while (tile != null) {
			result += tile.content().name();
			tile = p.getSpot(tile.getX() + (whichWay == Direction.ACROSS ? 1 : 0), tile.getY() + (whichWay == Direction.DOWN ? -1 : 0));
		}
		
		return result;
	}

}

enum Direction {
	DOWN, ACROSS,
}
